package com.demo.petshop.controller;

import com.demo.petshop.entity.Addresses;
import com.demo.petshop.service.AddressesService;
import com.demo.petshop.util.Result;
import com.mybatisflex.core.paginate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 *  控制层。
 *
 * @author lenovo
 * @since 2025-06-11
 */
@RestController
@RequestMapping("/addresses")
public class AddressesController {

    @Autowired
    private AddressesService addressesService;

    @PostMapping("addAddress")
    public Result saveAddress(@RequestBody Addresses addresses) {
        return Result.success(addressesService.saveAddress(addresses));
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("deleteAddress/{id}")
    public Result remove(@PathVariable Serializable id) {
        return Result.success(addressesService.removeById(id));
    }

    @PutMapping("updateAddress")
    public Result update(@RequestBody Addresses addresses) {
        return Result.success(addressesService.updateAddress(addresses));
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public Result list() {
        return Result.success(addressesService.list());
    }

    @GetMapping("getUserAddresses")
    public Result getInfo(@RequestParam Integer userId) {
        return Result.success(addressesService.getUserAddresses(userId));
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<Addresses> page(Page<Addresses> page) {
        return addressesService.page(page);
    }

    @PutMapping("setDefaultAddress")
    public Result setDefaultAddress(@RequestParam Integer addressId,@RequestParam Integer userId){
        return Result.success(addressesService.setDefaultAddress(addressId,userId));
    }
}
